package com.lannuokeji.biz.items.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.lannuokeji.base.AController;
import com.lannuokeji.base.facade.PageRequest;
import com.lannuokeji.biz.items.entity.DeviceApply;
import com.lannuokeji.biz.items.request.DeviceApplyRequest;
import com.lannuokeji.biz.items.entity.DeviceApply;
import com.lannuokeji.biz.items.service.DeviceApplyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 申请设备 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-19
 */
@Api(description="设备借用申请")
@RestController
@RequestMapping("/items/deviceApply")
public class DeviceApplyController extends AController {

	@Resource
	DeviceApplyService deviceApplyService;
	
	@ApiOperation(value = "查询分页列表")
	@GetMapping()
	public Object pageData(PageRequest pageRequest) {
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = deviceApplyService.selectPage(page);
		return setSuccessContent(page);
	}
	
	@ApiOperation(value = "分页查询申请列表，包括关联设备")
	@GetMapping("/withDevice")
	public Object pageDataWithDevice(PageRequest pageRequest, DeviceApply deviceApply) {
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = deviceApplyService.pageWithDevice(page, deviceApply);
		return setSuccessContent(page);
	}

	@ApiOperation(value = "添加设备申请")
	@PostMapping()
	public Object create(HttpServletRequest request, @RequestBody DeviceApplyRequest deviceApplyRequest) {
		if (deviceApplyRequest.getDevices() != null && deviceApplyRequest.getDevices().size() <= 0) {
			return setErrorContent("请选择要申请借用的设备");
		}
		boolean ret = deviceApplyService.createApplyWithDevice(deviceApplyRequest);
		if (false) {
			return setSuccessContent("添加成功！");
		} else {
			return setErrorContent("添加失败！");
		}
	}

	@ApiOperation(value = "根据id查询设备申请")
	@GetMapping("/{id}")
	public Object get(HttpServletRequest request,
			@ApiParam(value = "设备申请id", required = true) @PathVariable Integer id) {
		DeviceApply items = deviceApplyService.selectById(id);
		return setSuccessContent(items);
	}

	@ApiOperation(value = "删除设备申请信息")
	@DeleteMapping("/{id}")
	public Object del(HttpServletRequest request,
			@ApiParam(value = "设备申请id", required = true) @PathVariable Integer id) {
		if (deviceApplyService.deleteById(id)) {
			return setSuccessContent("删除成功");
		} else {
			return setErrorContent("删除失败");
		}
	}

	@ApiOperation(value = "修改设备申请信息")
	@PutMapping()
	public Object update(HttpServletRequest request, @RequestBody DeviceApply entity) {
		if (deviceApplyService.updateById(entity)) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}
	
	@ApiOperation(value = "审核、处理设备申请")
	@PutMapping("/applyCheck/{applyId}")
	public Object applyCheck(@ApiParam(value = "设备借用申请单id", required = true) @RequestParam int status,
			@ApiParam(value = "设备借用申请单id", required = true) @PathVariable Integer applyId) {
		DeviceApply entity = new DeviceApply();
		entity.setId(applyId);
		entity.setStatus(status);
		if (deviceApplyService.updateById(entity)) {
			return setSuccessContent("处理成功！");
		} else {
			return setErrorContent("处理失败！");
		}
	}
}

